package com.wsjc.web.entity;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RedisInfoDetail {
	private static Map<String, String> map = new HashMap<String, String>();
	
	static {
		map.put("redis_version", "Redis 服务器版本");
		map.put("role", "角色");
		
		map.put("uptime_in_days", "redis服务器启动总时间，单位是天");
		map.put("used_cpu_user", "用户空间占用CPU百分比");
		map.put("used_cpu_sys", "内核空间占用CPU百分比");
		map.put("connected_clients", "连接客户端数量");
		map.put("blocked_clients", "阻塞客户端数量(如BLPOP)");
		
		map.put("used_memory_human", "使用总内存");
		map.put("used_memory_rss", "使用物理内存");
		map.put("used_memory_peak_human", "内存消耗峰值");
		
		map.put("keys", "key的总数量");
		map.put("total_connections_received", "新创建连接个数");
//		map.put("total_commands_processed", "redis处理的命令数");
		map.put("instantaneous_ops_per_sec", "redis当前的qps");
		map.put("rejected_connections", "拒绝的连接个数，redis连接个数达到maxclients限制");
//		map.put("sync_full", "主从完全同步成功次数");
		map.put("expired_keys", "运行以来过期的key的数量");
		
		map.put("evicted_keys", "超过了maxmemory的key的数量");
//		map.put("latest_fork_usec", "最近一次fork操作阻塞redis进程的耗时数，单位微秒");
		map.put("keyspace_hits", "命中数量");
		map.put("keyspace_misses", "丢失数量");
//		map.put("keyspace_hitratio", "命中百分比");
		
//		map.put("aof_enabled", "是否开启了aof");
		map.put("aof_current_size", "AOF日志当前大小");
		
		map.put("cluster_enabled", "实例是否启用集群模式");
		
	}
	
	private String rv ;
	private String time = nowTime();
	private String role;
	private String us;
	private String sy;
	private String cl;
	private String bcl;
	private String mem;
	private String rss;
	private String umph;
	private String tcr;
	private String rc;
	private String keys;
	private String cmd;
	private String exp;
	private String evt;
	private String hit;
	private String mis;
	private String aofcs = "——";
    private String ce;
	
	
	private String key;
	private String value;
	private String desctiption;
	
	// 实例列表页
	private String address;
	private String type;
	//命中率
	private String mzl = "——";
	private String rtime;
	private String status;
	
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getMzl() {
		return mzl;
	}
	public void setMzl(String mzl) {
		this.mzl = mzl;
	}
	
	public String getRtime() {
		return rtime;
	}
	public void setRtime(String rtime) {
		this.rtime = rtime;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getCe() {
		return ce;
	}
	public void setCe(String ce) {
		this.ce = ce;
	}
	public String getRc() {
		return rc;
	}
	public void setRc(String rc) {
		this.rc = rc;
	}
	public String getTcr() {
		return tcr;
	}
	public void setTcr(String tcr) {
		this.tcr = tcr;
	}
	public String getUmph() {
		return umph;
	}
	public void setUmph(String umph) {
		this.umph = umph;
	}
	public String getKey() {
		return key;
	}
	private String nowTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        //获取String类型的时间
		return sdf.format(new Date());
	}
	public void setKey(String key) {
		this.key = key;
		this.desctiption = map.get(this.key);
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getDesctiption() {
		return desctiption;
	}
	public void setDesctiption(String desctiption) {
		this.desctiption = desctiption;
	}
	
	public boolean checkKey(String key) {
		return map.containsKey(key);
	}
	
	public static Map<String, String> getMap() {
		return map;
	}
	public static void setMap(Map<String, String> map) {
		RedisInfoDetail.map = map;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}
	public String getUs() {
		return us;
	}
	public void setUs(String us) {
		this.us = us;
	}
	public String getSy() {
		return sy;
	}
	public void setSy(String sy) {
		this.sy = sy;
	}
	public String getCl() {
		return cl;
	}
	public void setCl(String cl) {
		this.cl = cl;
	}
	public String getBcl() {
		return bcl;
	}
	public void setBcl(String bcl) {
		this.bcl = bcl;
	}
	public String getMem() {
		return mem;
	}
	public void setMem(String mem) {
		this.mem = mem;
	}
	public String getRss() {
		return rss;
	}
	public void setRss(String rss) {
		this.rss = getDouble(rss)+"K";
	}
	public String getKeys() {
		return keys;
	}
	public void setKeys(String keys) {
		this.keys = keys;
	}
	public String getCmd() {
		return cmd;
	}
	public void setCmd(String cmd) {
		this.cmd = cmd;
	}
	public String getExp() {
		return exp;
	}
	public void setExp(String exp) {
		this.exp = exp;
	}
	public String getEvt() {
		return evt;
	}
	public void setEvt(String evt) {
		this.evt = evt;
	}
	public String getHit() {
		return hit;
	}
	public void setHit(String hit) {
		this.hit = replaceBlank(hit);
	}
	public String getMis() {
		return mis;
	}
	public void setMis(String mis) {
		this.mis = replaceBlank(mis);
	}
	public String getAofcs() {
		return aofcs;
	}
	public void setAofcs(String aofcs) {
		this.aofcs = aofcs;
	}
	public String getRv() {
		return rv;
	}
	public void setRv(String rv) {
		this.rv = rv;
	}
	
	
	@Override
	public String toString() {
		return "RedisInfoDetail [time=" + time + ", role=" + role + ", us=" + us + ", sy=" + sy + ", cl=" + cl
				+ ", bcl=" + bcl + ", mem=" + mem + ", rss=" + rss + ", keys=" + keys + ", cmd=" + cmd + ", exp=" + exp
				+ ", evt=" + evt + ", hit=" + hit + ", mis=" + mis + ", aofcs=" + aofcs + ", key=" + key + ", value="
				+ value + ", desctiption=" + desctiption + "]";
	}
	
	/** 转double **/
	public static String getDouble(String num) {
		NumberFormat nbf=NumberFormat.getInstance(); 
		nbf.setMinimumFractionDigits(2); 
		String c = nbf.format(Double.valueOf(num)/1024);
		return c;
	}
	
	
	
	/** setValue() **/
	public void setValue(RedisInfoDetail infoDetail, String key, String value) {
		
		if(!map.containsKey(key)) {
			return ;
		}
        if(key.equals("redis_version")) {
        	infoDetail.rv = value; 
		}else if(key.equals("role")) {
			infoDetail.role = value; 
		}else if(key.equals("used_cpu_user")) {
			infoDetail.us = value; 
		}else if(key.equals("used_cpu_sys")) {
			infoDetail.sy = value; 
		}else if(key.equals("connected_clients")) {
			infoDetail.cl = value; 
		}else if(key.equals("blocked_clients")) {
			infoDetail.bcl = value; 
		}else if(key.equals("used_memory_human")) {
			infoDetail.mem = value; 
		}else if(key.equals("used_memory_rss")) {
			infoDetail.rss = getDouble(value)+"K"; 
		}else if(key.equals("used_memory_peak_human")) {
			infoDetail.umph = value; 
		}else if(key.equals("keys")) {
			infoDetail.keys = value; 
		}else if(key.equals("total_connections_received")) {
			infoDetail.tcr = value; 
		}else if(key.equals("rejected_connections")) {
			infoDetail.rc = value; 
		}else if(key.equals("expired_keys")) {
			infoDetail.exp = value; 
		}else if(key.equals("evicted_keys")) {
			infoDetail.evt = value; 
		}else if(key.equals("keyspace_hits")) {
			infoDetail.hit = replaceBlank(value); 
		}else if(key.equals("keyspace_misses")) {
			infoDetail.mis = replaceBlank(value); 
		}else if(key.equals("aof_current_size")) {
			infoDetail.aofcs = value; 
		}else if(key.equals("cluster_enabled")) {
			infoDetail.ce = value; 
		}else if(key.equals("instantaneous_ops_per_sec")) {
			infoDetail.cmd = value; 
		}else if(key.equals("uptime_in_days")) {
		    infoDetail.rtime = value; 
	    }
        
	}
	
	
	public static void main(String[] args) {
		
		String aa = "afasdfsdf" + "\r";
		System.out.println(aa);
		aa = replaceBlank(aa);
		System.out.println(aa);
		System.out.println("bbb");
	}
	
	
	public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
	
	
	
	
	
	
}
